package com.example.aussiehealthhelper;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class DrugManageAdapter extends RecyclerView.Adapter<DrugManageAdapter.DrugManageViewHolder> {

    //回调接口
    public interface DeleteClickListener {
        void onDeleteRequested(Drug drug, int position);
    }

    private final Context context;
    private List<Drug> drugList;
    private final HealthDbHelper dbHelper;
    private final DeleteClickListener deleteClickListener;

    public DrugManageAdapter(Context context, List<Drug> drugList, DeleteClickListener deleteClickListener) {
        this.context = context;
        this.drugList = drugList;
        this.dbHelper = new HealthDbHelper(context);
        this.deleteClickListener = deleteClickListener;
    }

    @NonNull
    @Override
    public DrugManageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_drug_manage, parent, false);
        return new DrugManageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DrugManageViewHolder holder, int position) {
        Drug drug = drugList.get(position);
        holder.tvDrugNameCN.setText(drug.getNameCN());
        holder.tvDrugNameEN.setText(drug.getNameEN());

        Log.d("DrugNote", "ID:" + drug.getId() +
                " Type:" + drug.getNote().getClass().getSimpleName() +
                " Value:" + drug.getNote());

        //显示备注
        if (drug.getNote() != null && !drug.getNote().isEmpty()) {
            holder.tvDrugNote.setText(drug.getNote());
        } else {
            holder.tvDrugNote.setText("-- 无备注信息 --");
        }

        holder.tvDrugIndication.setText("适应症: " + (drug.getIndication() != null ? drug.getIndication() : "暂无信息"));

        if (drug.getAdverseReaction() != null && !drug.getAdverseReaction().isEmpty()) {
            holder.tvDrugAdverse.setText("不良反应: " + drug.getAdverseReaction());
        } else {
            holder.tvDrugAdverse.setText("不良反应: 暂无信息");
        }

        //设置类似药品
        if (drug.getSimilarDrugs() != null && !drug.getSimilarDrugs().isEmpty()) {
            holder.tvDrugSimilar.setText("类似药品: " + drug.getSimilarDrugs());
        } else {
            holder.tvDrugSimilar.setText("无类似药品信息");
        }

        //按钮点击
        holder.btnEditDrug.setOnClickListener(v -> {
            Intent intent = new Intent(context, DrugEditActivity.class);
            intent.putExtra("drug_id", drug.getId());
            context.startActivity(intent);
        });

        holder.btnDeleteDrug.setOnClickListener(v -> {
            if (deleteClickListener != null) {
                deleteClickListener.onDeleteRequested(drug, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return drugList.size();
    }

    //更新数据
    public void updateDrugList(List<Drug> newDrugList) {
        drugList = newDrugList;
        notifyDataSetChanged();
    }

    static class DrugManageViewHolder extends RecyclerView.ViewHolder {
        TextView tvDrugNameCN, tvDrugNameEN, tvDrugNote, tvDrugIndication, // 从tvDrugRating改为tvDrugNote
                tvDrugAdverse, tvDrugSimilar;
        Button btnEditDrug, btnDeleteDrug;

        public DrugManageViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDrugNameCN = itemView.findViewById(R.id.tvDrugNameCN);
            tvDrugNameEN = itemView.findViewById(R.id.tvDrugNameEN);
            tvDrugNote = itemView.findViewById(R.id.tvDrugNote); // 从tvDrugRating改为tvDrugNote
            tvDrugIndication = itemView.findViewById(R.id.tvDrugIndication);
            tvDrugAdverse = itemView.findViewById(R.id.tvDrugAdverse);
            tvDrugSimilar = itemView.findViewById(R.id.tvDrugSimilar);
            btnEditDrug = itemView.findViewById(R.id.btnEditDrug);
            btnDeleteDrug = itemView.findViewById(R.id.btnDeleteDrug);
        }
    }
}